define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    
    var UE = require('lib/ueditor/ueditor.all');
    var uService = require('core/services/biz/noticeService');
    var getTimeToString = uService.getTimeToString;
    //require('../announcementPanelCtrl'); panel采用异步加载
    $.widget('rk.announcementCreateDialogCtrl', $.rk.oaDialogCtrl, {
        formContent: null,
        editor: {},
        _create:function(){
            var me = this;
            me._initListener();
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var temp = require('oa/tmpl/announcement/template/tpl_announcement_create.tpl');
            var html = rk.templateText(temp,{});
            //elem.html(rk.template('tpl_announcement_create', {}));
            elem.html(html);

            var footer = me.getFooter();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            footer.find('*[act="cancel"]').on('click', $.proxy(me.cancelClicked, me));


            //var editor = new ingage.editor.ui.Editor();
            //editor.render('announcementEditor');
            //me.editor = editor;

            var editor = new UE.ui.Editor({initialFrameHeight:300,initialFrameWidth:770});
            editor.render('announcementEditor');
            me.editor = editor;
            me._initData();
        },
        _initData: function () {
            var me = this;
            var elem = me.element;
            //加载公告部门
            rk.post("/json/oa_announcement/initAddGroup.action", {pageNo: 1, pageSize: 20}, function (json) {
                if (json.status === 0) {
                    var dataList = json.groupList;
                    var html = "";
                    $.each(dataList, function (i, dataJson) {
                        if (i == 0) {
                            html += "<option value='" + dataJson.id + "' selected>" + dataJson.name + "</option>";
                        } else {
                            html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                        }
                    });
                    elem.find("[act='announce-dep']").html(html);
                    if (dataList.length > 0) {
                        elem.find("[act='announce-dep']").val(dataList[0].id);
                        elem.find('[act="announce-dep"]').selectmenu('refresh', true);
                    }
                    dataList = json.typeList;
                    if (dataList.length > 0) {
                        var html = "";
                        $.each(dataList, function (i, dataJson) {
                            if (i == 0) {
                                html += "<option value='" + dataJson.id + "' selected>" + dataJson.name + "</option>";
                            } else {
                                html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                            }
                        });
                        elem.find("[act='announce-type']").html(html);
                        elem.find("[act='announce-type']").val(dataList[0].id);
                        elem.find('[act="announce-type"]').selectmenu('refresh', true);
                    } else {
                        elem.find("[act='anno_type_div']").hide();
                    }
                } else {
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                }
            });
            //加载公告类型
            /*rk.post("/json/oa_announcement/initType.action", {pageNo: 1, pageSize: 20}, function (json) {
                if (json.status === 0) {
                    var dataList = json.typeList;
                    if (dataList.length > 0) {
                        var html = "";
                        $.each(dataList, function (i, dataJson) {
                            if (i == 0) {
                                html += "<option value='" + dataJson.id + "' selected>" + dataJson.name + "</option>";
                            } else {
                                html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                            }
                        });
                        elem.find("[act='announce-type']").html(html);
                        elem.find("[act='announce-type']").val(dataList[0].id);
                        elem.find('[act="announce-type"]').selectmenu('refresh', true);
                    } else {
                        elem.find("[act='anno_type_div']").hide();
                    }
                } else {
                    rk.noticeError("系统错误");
                }
            });*/
            elem.find('[act="announce-dep"]').selectmenu();
            elem.find('[act="announce-type"]').selectmenu();


            //加载置顶时间
            var stick_days = {
                3: rk.i18n('ANNOUNCEMENT_TIME_THREE_DAY'),
                7: rk.i18n('ANNOUNCEMENT_TIME_ONE_WEEK'),
                14: rk.i18n('ANNOUNCEMENT_TIME_TWO_WEEK'),
                30: rk.i18n('ANNOUNCEMENT_TIME_ONE_MONTH'),
                60: rk.i18n('ANNOUNCEMENT_TIME_TWO_MONTH'),
                90: rk.i18n('ANNOUNCEMENT_TIME_THREE_MONTH'),
                180: rk.i18n('ANNOUNCEMENT_TIME_SIX_MONTH'),
                360: rk.i18n('ANNOUNCEMENT_TIME_ONE_YEAR'),
                "-1": rk.i18n('ANNOUNCEMENT_TIME_FOREVER')
            };
            var stick_daysHtml = "";
            for (var key in stick_days) {
                stick_daysHtml += "<option value='" + key + "'>" + stick_days[key] + "</option>"
            }
            elem.find("[act='anno_stick_days']").html(stick_daysHtml);
            elem.find("[act='anno_stick_days']").val(3);
            elem.find("[act='anno_stick_days']").selectmenu();
            elem.find("[act='anno_stick_date']").datepicker();
            elem.find(".ui-check-buttonset").buttonset();
            elem.find(".ui-radio-buttonset").buttonset();
        },
        _initListener: function () {
            var me = this;
            var elem = me.element;
            elem.on("click", "[act='anno_stick_type_days_radio']", function () {
                elem.find("[act='anno_stick_days']").attr("disabled", null);
                elem.find("[act='anno_stick_date']").attr("disabled", "disabled");
            });
            elem.on("click", "[act='anno_stick_type_date_radio']", function () {
                elem.find("[act='anno_stick_days']").attr("disabled", "disabled");
                elem.find("[act='anno_stick_date']").attr("disabled", null);
            });
            elem.on("click", "[act='anno_stick_is_no']", function(){
                elem.find("[act='anno_stick_form']").toggle();
                rk.repositionDialog();
            });
        },
        _drawPrivateLeaveForm: function () {
            var me = this;
            var elem = me.element;
        },
        _drawSickLeaveForm: function () {
            var me = this;
            var elem = me.element;
        },
        okClicked: function () {
            var me = this;
            var elem = me.element;

            //rk.confirm('确定保存吗');
            me._doSave();//保存
            //me.closeMe();//关闭

        },
        cancelClicked: function () {
            var me = this;
            var elem = me.element;
            me.closeMe();//关闭
        },
        _doSave: function () {
            var me = this;
            var elem = me.element;
            var editor = me.editor;
            editor.sync("announcementSaveForm");



            var form = elem.find("[act='announcement_add_form']");
            var formData = form.serializeArray();
            var params = {};
            $.each(formData, function (i, json) {
                if (json.name === "stickDate" && json.value) {
                    var value = json.value;
                    var valueArray = value.split("-");
                    var year = valueArray[0];
                    var month = valueArray[1];
                    var day = valueArray[2];
                    json.value = new Date(year - 0, month - 1, day - 0).getTime();
                }
                params["announcement." + json.name] = json.value;
            });
            if (params["announcement.stick"] != 1) {
                //params;stickType  stickDays  stickDate
                delete params["announcement.stickType"];
                delete params["announcement.stickDays"];
                delete params["announcement.stickDate"];
            }
            if (!params["announcement.title"]) {
                rk.noticeError(rk.i18n('ANNOUNCEMENT_1'));
                return;
            }
            if (!editor.hasContents()) {
                rk.noticeError(rk.i18n('ANNOUNCEMENT_2'));
                return;
            }
            var content = editor.getContent();
            if(content.length > 8000000){
                rk.noticeError(rk.i18n('ANNOUNCEMENT_3'));
                return;
            }
            me.getBtn('ok').beLoading();
            rk.post("/json/oa_announcement/save-announcement.action", params, function (json) {
                if (json.status === 0) {
                    //新增成功
                    /*var panel = rk.oa.getPanel();
                    panel.announcementPanelCtrl("loaddata", "announcement");
                    me.closeMe();*/
                    //panel 同步加载出问题，只能异步加载
                    require.async(['../announcementPanelCtrl.js'],function(c) {
                        var panel = rk.oa.getPanel();
                        panel.announcementPanelCtrl("loaddata", "announcement");
                        me.closeMe();
                    });
                    rk.noticeSuccess(rk.i18n('OPERATE_SUCCESS'));
                } else if (json.status === 300002) {
                    rk.noticeError(rk.i18n('ANNOUNCEMENT_4'));
                    me.getBtn('ok').beLoading('delay');
                } else if (json.status === 211001) {
                    //没有权限
                    rk.noticeError(rk.i18n('NOTICE_NO_ACCESS'));
                    me.getBtn('ok').beLoading('delay');
                } else if (json.status === 100000) {
                    //系统错误
                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    me.getBtn('ok').beLoading('delay');
                }
            });
        }
    });
});